package com.wytec.xywzxw.configuration;

import com.alibaba.fastjson.JSON;
import com.wytec.xywzxw.bean.commonBean.CommonResult;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

public class MyException extends Exception{
    public MyException(String message){
        HttpServletResponse response = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getResponse();
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        try (PrintWriter printWriter = response.getWriter()) {
            printWriter.println(JSON.toJSON(CommonResult.failed(message)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}